home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 360 < prev    next >
Text File  |  1996-08-06  |  4KB  |  101 lines

  1. Newsgroups: comp.std.c
  2. Path: nntp.coast.net!torn!sq!msb
  3. From: msb@sq.com (Mark Brader)
  4. Subject: Re: Is a diagnostic required?
  5. Message-ID: <1996Feb23.090526.7591@sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <danpop.824999740@rscernix>
  8. Date: Fri, 23 Feb 1996 09:05:26 GMT
  9.  
  10. Dan Pop (danpop@mail.cern.ch) writes:
  11. > Does the following code require a diagnostic?
  12. >     foo() { }
  13. >     main() { foo(3); } > 
  14. > I was always convinced that there is no difference between foo() and
  15. > foo(void) in the _definition_ of the function foo ... Yet, all the
  16. > compilers I tried accepted this code without complaint and complained
  17. > when 'void' was introduced in the definition of foo.
  18.  
  19. The language of the standard is fuzzy here, but I believe that it is
  20. clearly intended for the diagnostic to be mandatory when the "void" is
  21. present, and optional otherwise (the behavior being undefined).
  22.  
  23. An excerpt from the Constraints of section 6.3.2.2/3.3.2.2 (emphasis added):
  24.  
  25. #  If the expression that denotes the called function has a type that
  26. #  INCLUDES a prototype, the number of arguments shall agree with the
  27. #  number of parameters.
  28.  
  29. As this is a Constraint, a violation requires a diagnostic.  But from the
  30. Semantics of the same section (emphasis added):
  31.  
  32. #  If the expression that denotes the called function has a type that
  33. #  DOES NOT include a prototype ...  If the number of arguments does
  34. #  not agree with the number of parameters, the behavior is undefined.
  35.  
  36. Thus no diagnostic is required if this is the only violation.  I must
  37. say it's rather disgusting that none of the compilers Dan tried was smart
  38. and helpful enough to produce the diagnostic anyway -- it would after
  39. all be easy enough for them to do so in this case.  (Cancel that remark
  40. if they were all compilers intended to be used with lint for checking.)
  41.  
  42. Now, "prototype" is defined in 6.1.2.1/3.1.2.1:
  43.  
  44. #  A "function prototype" is a declaration of a function that declares
  45. #  the types of its parameters.
  46.  
  47. And in case there was any doubt, both 6.5/3.5 and 6.7.1/3.7.1 make it
  48. clear that a definition is a kind of declaration.  I won't bother to
  49. quote them here.
  50.  
  51.  
  52. These passages are sloppy for the following reasons:
  53.  
  54. 1. "Function prototype" is defined, but the term used elsewhere in the
  55.    standard is just "prototype".
  56.  
  57. 2. It is defined as a syntactic construct, not something that might be
  58.    "included" in a type.
  59.  
  60. 3. If a function has no parameters, it is not possible to distinguish
  61.    whether they are declared or not.
  62.  
  63. 4. The old-style function definition
  64.  
  65.     void fu (x,y) short x,y; { }
  66.  
  67.    is a declaration that declares the types of the function's parameters,
  68.    but if this was deemed to be a prototype, existing code would be broken.
  69.  
  70.    The way to resolve both points 3 and 4 is to relate the term "prototype"
  71.    more directly to the new-style syntax, which it is obviously intended
  72.    to denote.  In section 6.5.4.3/3.5.4.3, where function declarators and
  73.    their semantics are actually defined, the term is avoided altogether and
  74.    "parameter type list" is used instead.  "Prototype" probably should have
  75.    been defined in these terms.  Note that void is a type, so foo(void)
  76.    would be a valid function declarator with a prototype, while foo()
  77.    would unambiguously not have a prototype.  There is no syntactic
  78.    ambiguity since a parameter type list is not allowed to be empty.
  79.  
  80. 5. The punctuation of the quoted passage from 6.3.2.2/3.3.2.2 Semantics
  81.    is ambiguous: it isn't clear whether the first If-clause quoted is
  82.    meant to govern the second one, which is in a different sentence.
  83.    More precisely, the punctuation suggests that it does not, but in
  84.    other places in the standard similarly punctuated, it would.  In
  85.    this case, however, material later in the paragraph suggests that
  86.    the last-quoted If-clause is not meant to be within the scope of the
  87.    other one.
  88.  
  89.    Fortunately, this parsing ambiguity makes no difference in this case;
  90.    the overall effect is the same with either parsing, no matter whether
  91.    a prototype is used or not.
  92.  
  93. If you take the intended readings as being what I said, then you end up
  94. with the conclusion I gave above.  If not, you probably don't.
  95. -- 
  96. Mark Brader, msb@sq.com       "I'm not a lawyer, but I'm pedantic and
  97. SoftQuad Inc., Toronto         that's just as good."      -- D Gary Grady
  98.  
  99. My text in this article is in the public domain.
  100.